<template>
    <div id="branchAdd" class="main-container" >
        <div>
            <el-form ref="branchForm" :model="branchForm"  :rules="rules" class="formEmbedTable mt20">  
                <table>
                    <caption>Add Department</caption>
                    <tr>
                        
                        <td>Parent Department</td>
                        <td>
                            <el-form-item prop="brhParentName">{{ branchForm.brhParentName }}</el-form-item>
                        </td>	
                        <td>Category</td>
                        <td>
                            <el-form-item prop="brhLevel">
                                {{ $tools.dict.BranchLevel(branchForm.brhLevel)}}
                            </el-form-item>
                        </td>				
                    </tr>
                    <tr>
                        <td>Department No.</td>
                        <td>
                            <el-form-item prop="brhId"><el-input v-model="branchForm.brhId" placeholder="Department No.">{{ branchForm.brhParentId }}</el-input></el-form-item>
                        </td>
                        <td>Status</td>
                        <td>
                            <el-form-item prop="brhStatus">
                                <el-radio v-model="branchForm.brhStatus" label="0">Active</el-radio>
                                <el-radio v-model="branchForm.brhStatus" label="1">Suspended</el-radio>
                            </el-form-item>
                        </td>
                    </tr>
                    <tr>
                        <td>Department Name</td>
                        <td>
                            <el-form-item prop="brhName"><el-input v-model="branchForm.brhName" placeholder="Department Name"></el-input></el-form-item>
                        </td>
                        <td>Abbreviation</td>
                        <td>
                            <el-form-item prop="brhShortName"><el-input v-model="branchForm.brhShortName"  placeholder="Abbreviation"></el-input></el-form-item>
                        </td>
                    </tr>
                    <tr>
                        <td>Couplet No.</td>
                        <td>
                            <el-form-item prop="unionNo"><el-input v-model="branchForm.unionNo" placeholder="Couplet No."></el-input></el-form-item>
                        </td>
                        <td>Liquidate No.</td>
                        <td>
                            <el-form-item prop="lquidateBRH">{{branchForm.unionNo}}</el-form-item>
                        </td>
                    </tr>
                    <tr>
                        <td>Phone No.</td>
                        <td>
                            <el-form-item prop="brhPhone" ><el-input v-model="branchForm.brhPhone"  value="" placeholder="TelePhone"></el-input></el-form-item>
                        </td>
                        
                        <td>Area Code</td>
                        <td>
                            <el-form-item ><el-input v-model="branchForm.brhRegionId"  placeholder="Area Code"></el-input></el-form-item>
                        </td>

                    </tr>
                    <tr>
                        <td>Address</td>
                        <td colspan="3">
                            <el-form-item prop="brhAddress"><el-input v-model="branchForm.brhAddress" placeholder="Address" ></el-input></el-form-item>
                        </td>
                    </tr>
                    <tr>
                        <td>Descripsion</td>
                        <td colspan="3">
                            <el-form-item prop="brhDesc"><el-input  v-model="branchForm.brhDesc"  placeholder="Descripsion" ></el-input></el-form-item>
                        </td>
                    </tr>
                </table>    
            </el-form>
        </div>
        <div class="btnArea mt20 tac">
            <el-button type="primary" icon="upload2" @click="queryAuth('branchForm')" >Confirm</el-button>
            <!--
            <el-button type="primary" icon="edit" @click="resetForm('branchForm')"></el-button>
            -->
            <el-button type="primary" icon="close" @click="back">Cancel</el-button>
        </div>
        <!--授权确认页面弹窗-->
        <div>
            <ynet-authDialog ref="dialogTable"  @getAuthForm="getAuthForm"></ynet-authDialog>
        </div>
    </div>
</template>
<script>
    import ghbIcon from '@src/components/icon'
    export default {
        name:'BranchAdd',
        components:{
			ghbIcon
		},
        props:[
            'formDataAdd',
        ],
        mounted(){
            this.branchForm=$.extend(this.branchForm,this.formDataAdd);
            //alert(111+"==="+JSON.stringify(this.branchForm));
        },
        data(){
            return {
                dialogTableVisible:false,
                authForm: { authUserNo: '', password: '',randomField:''},
                branchForm:{
                    brhName:'',brhType:'',brhTypeName:'',brhShortName:'',branchPNo:'',brhRegionId:'',lquidateBRH:'',
                    brhParentId:'',brhParentName:'',brhLevel:'',brhStatus:'0',channel:'CIM',unionNo:'',
                    brhPhone:'',brhRegionId:'',brhAddress:'',brhDesc:'',actionType:'0',bsnCode:'IM0101',brhId:'',
                    //branchNo:'043469', 
                },
                needAuth:'',
                authType:'',  
                rules:{
                    brhName:[
                        {required:true,message:'required',trigger:'blur'},
                    ],
                    brhShortName:[
                       {required:true,message:'required',trigger:'blur'},
                    ],
                    unionNo:[
                        {required:true,message:'required',trigger:'blur'},
                        {validator:this.$tools.validator.isNum,trigger:'blur',vm:this,formName:'branchForm'},
                    ],
                    // lquidateBRH:[
                    //    {required:true,message:'不能为空',trigger:'blur'},
                    //    {validator:this.$tools.validator.isNum,trigger:'blur'},
                    // ],
                    //brhAddress:{required:true,message:'required',trigger:'blur'},
                    brhId:[
                        {required:true,message:'required',trigger:'blur'},
                        {validator:this.$tools.validator.isNum,trigger:'blur'},
                    ],
                    brhRegionId:[
                        //{required:true,message:'required',trigger:'blur'},
                        //{validator:this.$tools.validator.isNum,trigger:'blur'}
                    ],
                    brhType:{required:true,message:'required',trigger:'blur'},
                    brhPhone:[
                        //{required:true,message:'required',trigger:'blur'},
                        { min: 8, max: 8, message: "length 8", trigger: "blur" },
                        {validator:this.$tools.validator.isHKPhone,trigger:'blur'}
                    ],
                    brhDesc:[{required:true,message:'required',trigger:'blur'}],
                }
            }
        },
        methods:{
            getAuthForm(val){
                //获取授权数据
                this.authForm=val;
                console.log("val:"+JSON.stringify(val))
                //将授权信息合并到提交给后台的对象中
                this.branchForm=$.extend(this.branchForm,this.authForm)
                this.submitForm()  //自己页面提交给后台的.do方法
            },
            queryAuth(formName){//查询授权模式
                this.$refs[formName].validate((valid)=>{
                    if(valid){
                        let body={};body.bsnCode="IM0101";body.version="IM0101",
                        this.$tools.request(this,"IM010001Action.do",body).then(data=>{
                            if(data.body.errorCode==0){
                                if(data.body.needAuth==0){//无需授权
                                    this.submitForm()
                                }else if(data.body.needAuth==1 && data.body.authType==1){
                                    this.needAuth=1;
                                    this.authType=1;
                                    this.$refs["dialogTable"].show()
                                }else if( data.body.needAuth==1 && data.body.authType==2 ){
                                    this.needAuth=1;
                                    this.authType=2;
                                    this.submitForm()
                                }else{
                                    this.$alert('Authorization type error', "Warning", {
                                        confirmButtonText: 'Confirm',
                                        callback: action => {
                                        }
                                    });
                                }
                            }else{
                                this.$alert("System Error Try again", "warning", "Warning", {
                                    confirmButtonText: 'Confirm',
                                    callback: action => {
                                    }
                                }); 
                            }
                            
                        })
                    }
                })
			},
            submitForm(){
                this.branchForm.lquidateBRH=this.branchForm.unionNo;
                //console.log("branchForm:"+JSON.stringify(this.branchAdd))
                this.$tools.request(this,"IM010102Action.do",this.branchForm).then(data=>{
                    if(data.body.errorCode==0){
                        this.$emit("back","isAdd")
                        if(this.needAuth==1 && this.authType==2){
                            this.$message({
								type:'success',
								message:'Waiting for Authorized',
							})
                        }else if(this.needAuth==1 && this.authType==1){
                            if(data.body.authStatus==1){
                                this.$message.error({
                                     message:'Authorship No. or password wrong ,please resume load',
                                    showClose:true,
                                })
                            }else{
                                this.$message({
                                    type:'success',
                                    message:'Add Success',
                                    showClose:true,
                                })
                            }
                        }else{
                            this.$message({
                                type:'success',
                                message:'Add Success',
                                showClose:true,
                            })
                        }
                        
                    }else{
                        this.$alert("System Error Try again", "warning",  {
                            confirmButtonText: 'Confirm',
                            callback: action => {
                            }
                        }); 
                    }
                })
                
            },
            resetForm(formName){
                this.$refs[formName].resetFields();
            },
            back(){
               this.$emit('back','isAdd');
            }
        }

    }
</script>